﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ScpControl;

namespace ScpServer
{
    public partial class BetterDS4Mapper : Form
    {
        private int device;
        private string profile;
        private int[] comboDefaults;

        public BetterDS4Mapper(int d, string p) {
            InitializeComponent();
            this.FormClosing += BetterDS4Mapper_FormClosing;

            device = d;
            profile = p;
        }

        public void FillDropdowns(string[] inputs)
        {
            cmbTriangle.Items.AddRange(inputs);
            cmbCircle.Items.AddRange(inputs);
            cmbCross.Items.AddRange(inputs);
            cmbSquare.Items.AddRange(inputs);
            cmbStart.Items.AddRange(inputs);
            cmbSelect.Items.AddRange(inputs);
            cmbPS.Items.AddRange(inputs);
            cmbL1.Items.AddRange(inputs);
            cmbL2.Items.AddRange(inputs);
            cmbR1.Items.AddRange(inputs);
            cmbR2.Items.AddRange(inputs);
            cmbUp.Items.AddRange(inputs);
            cmbRight.Items.AddRange(inputs);
            cmbDown.Items.AddRange(inputs);
            cmbLeft.Items.AddRange(inputs);
            cmbLeftStickX.Items.AddRange(inputs);
            cmbLeftStickY.Items.AddRange(inputs);
            cmbRightStickX.Items.AddRange(inputs);
            cmbRightStickY.Items.AddRange(inputs);
            cmbL3.Items.AddRange(inputs);
            cmbR3.Items.AddRange(inputs);

            if (profile == "")
            {
                cmbTriangle.SelectedIndex = 0;
                cmbCircle.SelectedIndex = 1;
                cmbCross.SelectedIndex = 2;
                cmbSquare.SelectedIndex = 3;
                cmbStart.SelectedIndex = 4;
                cmbSelect.SelectedIndex = 5;
                cmbPS.SelectedIndex = 6;
                cmbL1.SelectedIndex = 7;
                cmbL2.SelectedIndex = 8;
                cmbR1.SelectedIndex = 9;
                cmbR2.SelectedIndex = 10;
                cmbUp.SelectedIndex = 11;
                cmbRight.SelectedIndex = 12;
                cmbDown.SelectedIndex = 13;
                cmbLeft.SelectedIndex = 14;
                cmbLeftStickX.SelectedIndex = 15;
                cmbLeftStickY.SelectedIndex = 16;
                cmbRightStickX.SelectedIndex = 17;
                cmbRightStickY.SelectedIndex = 18;
                cmbL3.SelectedIndex = 19;
                cmbR3.SelectedIndex = 20;
            }
            else
            {
                cmbTriangle.SelectedIndex = Global.getCurrentMappings(device, 0);
                cmbCircle.SelectedIndex = Global.getCurrentMappings(device, 1);
                cmbCross.SelectedIndex = Global.getCurrentMappings(device, 2);
                cmbSquare.SelectedIndex = Global.getCurrentMappings(device, 3);
                cmbStart.SelectedIndex = Global.getCurrentMappings(device, 4);
                cmbSelect.SelectedIndex = Global.getCurrentMappings(device, 5);
                cmbPS.SelectedIndex = Global.getCurrentMappings(device, 6);
                cmbL1.SelectedIndex = Global.getCurrentMappings(device, 7);
                cmbL2.SelectedIndex = Global.getCurrentMappings(device, 8);
                cmbR1.SelectedIndex = Global.getCurrentMappings(device, 9);
                cmbR2.SelectedIndex = Global.getCurrentMappings(device, 10);
                cmbUp.SelectedIndex = Global.getCurrentMappings(device, 11);
                cmbRight.SelectedIndex = Global.getCurrentMappings(device, 12);
                cmbDown.SelectedIndex = Global.getCurrentMappings(device, 13);
                cmbLeft.SelectedIndex = Global.getCurrentMappings(device, 14);
                cmbLeftStickX.SelectedIndex = Global.getCurrentMappings(device, 15);
                cmbLeftStickY.SelectedIndex = Global.getCurrentMappings(device, 16);
                cmbRightStickX.SelectedIndex = Global.getCurrentMappings(device, 17);
                cmbRightStickY.SelectedIndex = Global.getCurrentMappings(device, 18);
                cmbL3.SelectedIndex = Global.getCurrentMappings(device, 19);
                cmbR3.SelectedIndex = Global.getCurrentMappings(device, 20);
            }

            int[] comboSettings = {
                cmbTriangle.SelectedIndex,
                cmbCircle.SelectedIndex,
                cmbCross.SelectedIndex,
                cmbSquare.SelectedIndex,
                cmbStart.SelectedIndex,
                cmbSelect.SelectedIndex,
                cmbPS.SelectedIndex,
                cmbL1.SelectedIndex,
                cmbL2.SelectedIndex,
                cmbR1.SelectedIndex,
                cmbR2.SelectedIndex,
                cmbUp.SelectedIndex,
                cmbRight.SelectedIndex,
                cmbDown.SelectedIndex,
                cmbLeft.SelectedIndex,
                cmbLeftStickX.SelectedIndex,
                cmbLeftStickY.SelectedIndex,
                cmbRightStickX.SelectedIndex,
                cmbRightStickY.SelectedIndex,
                cmbL3.SelectedIndex,
                cmbR3.SelectedIndex
            };

            comboDefaults = comboSettings;
        }

        public void SetCurrentMappings () {
            Global.setCurrentMappings(device, 0, cmbTriangle.SelectedIndex);
            Global.setCurrentMappings(device, 1, cmbCircle.SelectedIndex);
            Global.setCurrentMappings(device, 2, cmbCross.SelectedIndex);
            Global.setCurrentMappings(device, 3, cmbSquare.SelectedIndex);
            Global.setCurrentMappings(device, 4, cmbStart.SelectedIndex);
            Global.setCurrentMappings(device, 5, cmbSelect.SelectedIndex);
            Global.setCurrentMappings(device, 6, cmbPS.SelectedIndex);
            Global.setCurrentMappings(device, 7, cmbL1.SelectedIndex);
            Global.setCurrentMappings(device, 8, cmbL2.SelectedIndex);
            Global.setCurrentMappings(device, 9, cmbR1.SelectedIndex);
            Global.setCurrentMappings(device, 10, cmbR2.SelectedIndex);
            Global.setCurrentMappings(device, 11, cmbUp.SelectedIndex);
            Global.setCurrentMappings(device, 12, cmbRight.SelectedIndex);
            Global.setCurrentMappings(device, 13, cmbDown.SelectedIndex);
            Global.setCurrentMappings(device, 14, cmbLeft.SelectedIndex);
            Global.setCurrentMappings(device, 15, cmbLeftStickX.SelectedIndex);
            Global.setCurrentMappings(device, 16, cmbLeftStickY.SelectedIndex);
            Global.setCurrentMappings(device, 17, cmbRightStickX.SelectedIndex);
            Global.setCurrentMappings(device, 18, cmbRightStickY.SelectedIndex);
            Global.setCurrentMappings(device, 19, cmbL3.SelectedIndex);
            Global.setCurrentMappings(device, 20, cmbR3.SelectedIndex);
            Global.saveRumbleBoost(device, (byte)tbrRumbleBoost.Value);

            int[] comboSettings = {
                cmbTriangle.SelectedIndex,
                cmbCircle.SelectedIndex,
                cmbCross.SelectedIndex,
                cmbSquare.SelectedIndex,
                cmbStart.SelectedIndex,
                cmbSelect.SelectedIndex,
                cmbPS.SelectedIndex,
                cmbL1.SelectedIndex,
                cmbL2.SelectedIndex,
                cmbR1.SelectedIndex,
                cmbR2.SelectedIndex,
                cmbUp.SelectedIndex,
                cmbRight.SelectedIndex,
                cmbDown.SelectedIndex,
                cmbLeft.SelectedIndex,
                cmbLeftStickX.SelectedIndex,
                cmbLeftStickY.SelectedIndex,
                cmbRightStickX.SelectedIndex,
                cmbRightStickY.SelectedIndex,
                cmbL3.SelectedIndex,
                cmbR3.SelectedIndex
            };

            comboDefaults = comboSettings;
        }

        void BetterDS4Mapper_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (tbxProfileName.Text != "")
            {
                if (e.CloseReason == CloseReason.UserClosing)
                {
                    if (
                        cmbTriangle.SelectedIndex == comboDefaults[0] &&
                        cmbCircle.SelectedIndex == comboDefaults[1] &&
                        cmbCross.SelectedIndex == comboDefaults[2] &&
                        cmbSquare.SelectedIndex == comboDefaults[3] &&
                        cmbStart.SelectedIndex == comboDefaults[4] &&
                        cmbSelect.SelectedIndex == comboDefaults[5] &&
                        cmbPS.SelectedIndex == comboDefaults[6] &&
                        cmbL1.SelectedIndex == comboDefaults[7] &&
                        cmbL2.SelectedIndex == comboDefaults[8] &&
                        cmbR1.SelectedIndex == comboDefaults[9] &&
                        cmbR2.SelectedIndex == comboDefaults[10] &&
                        cmbUp.SelectedIndex == comboDefaults[11] &&
                        cmbRight.SelectedIndex == comboDefaults[12] &&
                        cmbDown.SelectedIndex == comboDefaults[13] &&
                        cmbLeft.SelectedIndex == comboDefaults[14] &&
                        cmbLeftStickX.SelectedIndex == comboDefaults[15] &&
                        cmbLeftStickY.SelectedIndex == comboDefaults[16] &&
                        cmbRightStickX.SelectedIndex == comboDefaults[17] &&
                        cmbRightStickY.SelectedIndex == comboDefaults[18] &&
                        cmbL3.SelectedIndex == comboDefaults[19] &&
                        cmbR3.SelectedIndex == comboDefaults[20] &&
                        tbxProfileName.Text == profile
                    )
                    {
                        //Close
                    }
                    else
                    {
                        DialogResult mBox = MessageBox.Show("Would you like to save changes to " + tbxProfileName.Text + "?", this.Text, MessageBoxButtons.YesNoCancel);

                        switch (mBox)
                        {
                            case System.Windows.Forms.DialogResult.Yes:
                                btnSave_Click(sender, e);
                                break;
                            case System.Windows.Forms.DialogResult.No:
                                //Do Nothing
                                break;
                            case System.Windows.Forms.DialogResult.Cancel:
                                e.Cancel = true;
                                break;
                        }
                    }
                }
            }
        }

        private void BetterDS4Mapper_Load(object sender, EventArgs e)
        {
            AcceptButton = btnSave;

            tbxProfileName.Text = profile;
            btnSave.Enabled = (tbxProfileName.Text != "");

            string[] inputs = { "Y", "B", "A", "X", "Start", "Back", "Guide", "LB", "Left Trigger", "RB", "Right Trigger", "DPad Up", "DPad Right", "DPad Down", "DPad Left", "Left Stick (X)", "Left Stick (Y)", "Right Stick (X)", "Right Stick (Y)", "L3", "R3" };
            FillDropdowns(inputs);

            tbrRumbleBoost.Value = Global.loadRumbleBoost(device);
            tbrRumbleBoost_Scroll(sender, e);
            //Global.LoadProfile(device);
        }

        private void tbxProfileName_TextChanged(object sender, EventArgs e)
        {
            btnSave.Enabled = (tbxProfileName.Text != "");
        }

        private void tbrRumbleBoost_Scroll(object sender, EventArgs e) {
            tbrRumbleBoost.Value = (int) Math.Round((decimal) tbrRumbleBoost.Value / 10) * 10;
            lblRumbleBoost.Text = tbrRumbleBoost.Value + "%";
        }

        private void btnSave_Click(object sender, EventArgs e) {
            SetCurrentMappings();

            if (tbxProfileName.Text == profile)
            {
                Global.SaveProfile(device, tbxProfileName.Text);
                profile = tbxProfileName.Text;
                this.Close();
            }
            else
            {
                if (profile != "" && File.Exists(@"Profiles\" + profile + ".ds4")) {
                    File.Delete(@"Profiles\" + profile + ".ds4");
                }

                if (File.Exists(@"Profiles\" + tbxProfileName.Text+".ds4")) {
                    DialogResult mBox = MessageBox.Show("A profile with this name already exists, would you like to overwrite it?", this.Text, MessageBoxButtons.YesNo);

                    switch (mBox)
                    {
                        case System.Windows.Forms.DialogResult.Yes:
                            Global.SaveProfile(device, tbxProfileName.Text);
                            profile = tbxProfileName.Text;
                            this.Close();
                            break;
                        case System.Windows.Forms.DialogResult.No:
                            //Do Nothing
                            break;
                    }
                } else {
                    Global.SaveProfile(device, tbxProfileName.Text);
                    profile = tbxProfileName.Text;
                    this.Close();
                }
            }
        }
    }
}
